package com.styytech.dadabao.widge.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.styytech.dadabao.R;


/**
 * Created by user on 2015/8/5.
 * 使用例子在com.styytech.dadabase.utils.UpdateAppUtils.showDialog();
 */
public class DownloadDialog {
    private Dialog mDialog;
    private Context mContext;
    private ImageView iv_progreess_image;
    private TextView tv_progress_title,tv_progress_text,tv_percent;
    private LinearLayout llyt_progress_text;
    private ProgressBar myView_ProgressBar;
    //判断是否有详细文字 0是默认没有
    private int type=0 ;
    private View view;
    private String title;
    private Drawable drawable;
    private String text;
    /**
     * 没有详细内容使用默认icon的dialog
     * 上下文对象、title*/
    public DownloadDialog(Context context,String title){
        this.mContext=context;
        this.title=title;
        initViews();
    }
    /**
     * 没有详细内容不使用默认icon的dialog
     * 上下文对象、title drawable
     * */
    public DownloadDialog(Context context,String title,Drawable drawable){
        this.mContext=context;
        this.drawable=drawable;
        this.title=title;
        initViews();
    }
    /**
     * 有详细内容使用默认icon的dialog
     * type为1时表示有详细内容
     *上下文对象 title type为1时表示有详细内容 详细内容*/
    public DownloadDialog(Context context,String title,int type,String text){
        this.mContext=context;
        this.title=title;
        this.type=type;
        this.text=text;
        initViews();
    }
    /**
     * 有详细内容不使用默认icon的dialog
     * type为1时表示有详细内容
     * 上下文对象 title drawable type为1时表示有详细内容 详细内容*/
    public DownloadDialog(Context context,String title,Drawable drawable,int type,String text){
        this.mContext=context;
        this.drawable=drawable;
        this.title=title;
        this.type=type;
        this.text=text;
        initViews();
    }
    private void initViews(){
        mDialog = new Dialog(mContext,
                R.style.ensure_dialog);
        LayoutInflater layoutInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.dialog_progress, null);
        iv_progreess_image=(ImageView)view.findViewById(R.id.iv_progreess_image);
        tv_progress_title=(TextView)view.findViewById(R.id.tv_progress_title);
        llyt_progress_text=(LinearLayout)view.findViewById(R.id.llyt_progress_text);
        tv_progress_text=(TextView)view.findViewById(R.id.tv_progress_text);
        tv_percent=(TextView)view.findViewById(R.id.tv_percent);
        myView_ProgressBar=(ProgressBar)view.findViewById(R.id.myView_ProgressBar);
        /**
         * 根据用户传递的参数，确定布局控件的显示*/
        setVisible();
    }
    private void setVisible(){
        //判断是否有详细内容
        if(type==0){
            llyt_progress_text.setVisibility(View.GONE);
        }
        if(drawable==null){
            iv_progreess_image.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
        }else{
            iv_progreess_image.setBackgroundDrawable(drawable);
        }
        if (title!=null&&!title.equals("")){
            tv_progress_title.setText(title);
        }else{
            tv_progress_title.setText("下载");
        }
        if (text!=null&&!text.equals("")){
            tv_progress_text.setText(text);
        }
        setView();
    }
    //ProgressBar 的进度
    public void setProgress(int progress){
        myView_ProgressBar.setProgress(progress);
    }
    //显示进度的百分比
    public void setPercent(String percent){
        tv_percent.setText(percent);
    }
    private void setView(){
        myView_ProgressBar.setMax(100);
       // mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        mDialog.setContentView(view);

        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager m = ((Activity)mContext).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
      //  p.height = (int) (d.getHeight() * 0.25); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.65
        dialogWindow.setAttributes(lp);

    }
    /**
     * 设置点击屏幕或者back返回键能否取消*/
    public void NotCancel(boolean cancel){
        mDialog.setCanceledOnTouchOutside(cancel);//false设置点击屏幕其他地方，dialog不消失
        mDialog.setCancelable(cancel);//false设置点击返回键和HOme键，dialog不消失
    }
    public void show(){
        mDialog.show();
    }

    public void dismiss(){
        mDialog.dismiss();
    }
}
